package com.fernfx.xingtan.common.pullrefresh;

import android.view.View;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.AppManager;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.entity.HaveDomainEntity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.user.ui.LoginActivity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.fernfx.xingtan.utils.UIUtil;
import com.fernfx.xingtan.view.dialog.AlertDialog;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2018/8/20.
 */

public class PullToRefreshPresenter<P extends PullToRefreshContract.View> extends BaseIRequestCallback implements PullToRefreshContract.Presenter {

    private P P;
    private String requestUrl;
    //model
    private PullToRefreshContract.Model model;

    @Override
    public void init(BaseView view) {
        this.P = (P)view;
        this.model = new PullToRefreshModel();
    }

    @Override
    public void request(String requestUrl, Map<String, Object> requestArgsMap, boolean isGet) {
        P.getActivity().showLoading();
        model.request(requestUrl,requestArgsMap,this,isGet);
    }

    @Override
    public void detachView() {
        if (model != null) {
            model = null;
        }
    }

    @Override
    public void returnDomain(final String territoryId, final HaveDomainEntity.ObjBean.RecordsBean haveDomainBean, final TextView clickTv) {
        final AlertDialog alertDialog = new AlertDialog(P.getActivity())
                .builder()
                .setTitle("确定要退还领土吗？")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Map<String,Object> requestArgsMap = new HashMap<>(1);
                        requestArgsMap.put("territoryId",territoryId);
                        P.getActivity().showLoading();
                        model.returnDomain(requestArgsMap, new BaseIRequestCallback() {
                            @Override
                            public void onSuccessful(NetworkOKResult result) {
                                P.getActivity().hideLoading();
                                String message = result.getMessage();
                                BaseEntity baseEntity = FastJsonUtil.fromBean(message,BaseEntity.class);
                                if (OtherUtil.checkRequestStatus(baseEntity)) {
                                    haveDomainBean.setState(2);
                                    clickTv.setText("待审核");
                                    clickTv.setClickable(false);
                                    clickTv.setEnabled(false);
                                    clickTv.setTextColor(UIUtil.getColor(R.color.white));
                                }else {
                                    ToastUtil.showCentertoast(baseEntity.getMsg());
                                }
                            }
                        });
                    }
                })
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });
        alertDialog.show();
    }


    @Override
    public void onSuccessful(NetworkOKResult result) {
        P.getActivity().hideLoading();
        String message = result.getMessage();
        BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
        if (OtherUtil.checkRequestStatus(baseEntity)) {
            P.showHistroy(message);
        }else {
            ToastUtil.showCentertoast(baseEntity.getMsg());
        }
    }
}
